﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UMLTouch.Business.MVC;
using UMLTouch.Business;
using System.Configuration;
using UMLTouch.DomainRecognition;
using UMLTouch.Core.Entities;
using System.Windows.Ink;
using System.Windows.Threading;
using UMLTouch.Core.UI;

namespace UMLTouch.UI.Main
{
    /// <summary>
    /// Interaction logic for mainUI.xaml
    /// </summary>
    public partial class mainUI : Window, IUMLTouchView
    {
        public IUMLTouchControler Controler
        {
            get;
            protected set;
        }
        public IUMLTouchModel Model
        {
            get;
            protected set;
        }

        protected Dictionary<String, InkCanvas> _drawingContexts { get; set; }
        public Dictionary <String,Canvas> _contexts { get; set; }



        void IUMLTouchView.AddControl(Control control, string context)
        {

        }



        public mainUI()
        {
            InitializeComponent();
            _drawingContexts = new Dictionary<string, InkCanvas>();
            _contexts = new Dictionary<string, Canvas>();
        }

        public mainUI(IUMLTouchControler controler, IUMLTouchModel model)
            : this()
        {
            Controler = controler;
            Model = model;
            Model.ItemRecognized += new EventHandler<ItemAddedEventArgs>(Model_ItemRecognized);
            Model.OnUnknownItemDetected += new EventHandler<UnknownItemAddedEventArgs>(Model_OnUnknownItemDetected);
            Model.DiagramContextAdded += new EventHandler<DiagramContextAddedEventArgs>(Model_DiagramContextAdded);
        }

        void Model_OnUnknownItemDetected(object sender, UnknownItemAddedEventArgs e)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                InkCanvas context = null;
                if (_drawingContexts.TryGetValue(e.Context.Name, out context))
                {
                    context.Strokes.Clear();
                }

                MessageBox.Show("unknownItem");
            }
            ));
        }

        void Model_DiagramContextAdded(object sender, DiagramContextAddedEventArgs e)
        {
            String key = e.Context.Name;
            TabItem newTab = new TabItem()
            {
                Header = key
            };
         
            
            newTab.Content =  new DiagramControl(this, key);
            tc_diagrams.Items.Add(newTab);

           
        }


        void Model_ItemRecognized(object sender, ItemAddedEventArgs e)
        {
            Dispatcher.Invoke(new Action(() =>
                {
                    InkCanvas context = null;
                    if (_drawingContexts.TryGetValue(e.Context.Name, out context))
                    {
                        context.Strokes.Clear();
                        Canvas canvas = null;
                        if (_contexts.TryGetValue(e.Context.Name, out canvas))
                        {
                            canvas.Children.Add(e.Control);
                        }
                    }
                  
                }
             ));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Controler.CreateDiagramContext("ClassDiagramRecognition");
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            var contexts = tc_diagrams.Items.OfType<TabItem>().Select(ti => ti.Content as DiagramControl);

            foreach (var item in contexts)
            {
                item.switchEditMode();
            }
        }




    }
}
